package spring.cloud.tasks.log;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.Logger;

import java.net.URL;
import java.util.Map;
import java.util.Properties;

public class NameSpaceLoggerContext {
    private final String nameSpace;
    private final LoggerContext loggerContext;

    public NameSpaceLoggerContext(String nameSpace, LoggerContext loggerContext, Properties properties, URL url) {
        this.nameSpace = nameSpace;
        this.loggerContext = loggerContext;
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            loggerContext.putProperty((String) entry.getKey(), (String) entry.getValue());
        }
        try {
            new ContextInitializer(loggerContext).configureByResource(url);
        } catch (JoranException e) {
            throw new IllegalStateException(e);
        }
    }

    public Logger getLogger(Class aClass) {
        return loggerContext.getLogger(aClass);
    }
}
